﻿using KludgeBox.Events;
using KludgeBox.Events.Global;
using KludgeBox.Net;
using KludgeBox.Net.Packets;

namespace VectorSurvivors.Multiplayer;

public static class MultiplayerHelper
{
    private static EventPublisher<SendPacketToServerRequest> _sendToServer = EventBus.GetEventPublisher<SendPacketToServerRequest>();
    private static EventPublisher<SendPacketToClientsRequest> _sendToClients = EventBus.GetEventPublisher<SendPacketToClientsRequest>();
    
    public static void SendPacketToServer(AbstractPacket packet, bool forceReliable = false, bool forceNow = false)
    {
        _sendToServer.Publish(new(packet, forceReliable, forceNow));
    }
    
    public static void SendPacketToClients(AbstractPacket packet, bool forceReliable = false, bool forceNow = false)
    {
        _sendToClients.Publish(new(packet, forceReliable, forceNow));
    }
    
    public static void SendPacketToClient(long peerId, AbstractPacket packet, bool forceReliable = false)
    {
        if(!Network.IsServer) return;
        Network.SendPacketToPeer(peerId, packet, packet.IsReliable || forceReliable);
    }
}